//
//  BuzhouCollectionViewCell.m
//  B段项目
//
//  Created by 徐茂怀 on 15/11/5.
//  Copyright © 2015年 徐茂怀. All rights reserved.
//

#import "BuzhouCollectionViewCell.h"
#import "BuzhouTableViewCell.h"
#define POSTURL @"http://api.haodou.com/index.php?appid=2&appkey=9ef269eec4f7a9d07c73952d06b5413f&format=json&sessionid=1446688240625&vc=77&vn=5.4.0&loguid=0&deviceid=haodou99000505722929&uuid=a6afb132369a979ba3427eec69189063&channel=xiaomi_v540&method=Info.getVideoUrl&virtual=&signmethod=md5&v=2&timestamp=1446688349&nonce=0.5325869858665556&appsign=f4355cc56ef4008ca0dafe48a510ffcf"
#import "XMHNetWorkongMethods.h"
#import "SingleTon.h"
#import "FitHeader.h"
#import "BuzhouTableViewCell.h"
@implementation BuzhouCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self getData];
        [self createView];
    }
    return self;
}

-(void)createView
{
    _tableView = [[UITableView alloc]init];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    UIView *v = [[UIView alloc]initWithFrame:CGRectZero];
    _tableView.tableFooterView = v;
    
    [self.contentView addSubview:_tableView];
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    _tableView.frame = CGRectMake(0, 0, 375 * FitWidth, self.contentView.frame.size.height -64 *FitHeight);
}

-(void)getData
{
    [XMHNetWorkongMethods getDataVyString:POSTURL BodyString:[NSDictionary dictionaryWithObjectsAndKeys:[SingleTon shareSingleTon].rid,@"rid", nil] WithDataBlock:^(id data) {
        NSDictionary *dic = [data objectForKey:@"result"];
        _dataArray = [dic objectForKey:@"Steps"];
        [_tableView reloadData];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15 * FitWidth], NSFontAttributeName, nil];
    CGRect rect = [[_dataArray[indexPath.row]objectForKey:@"Content" ]boundingRectWithSize:CGSizeMake(265 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return rect.size.height + 20 * FitHeight;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    BuzhouTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[BuzhouTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [cell setContentString:[_dataArray[indexPath.row]objectForKey:@"Content"]];
    cell.numberLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    return cell;
}

@end
